<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ include file="../common/header.jsp" %>
<div class="main-content">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">播种采收管理</h1>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
            <i class="fa-solid fa-plus-circle"></i> 新增记录
        </button>
    </div>

    <!-- 筛选表单 -->
    <form class="row g-3 mb-3" method="get" action="">
        <div class="col-md-2">
            <label class="form-label">记录ID</label>
            <input type="text" class="form-control" name="sowHarvestId" value="${sowHarvestId}">
        </div>
        <div class="col-md-2">
            <label class="form-label">基地ID</label>
            <input type="number" class="form-control" name="baseId" value="${baseId}">
        </div>
        <div class="col-md-2">
            <label class="form-label">播种时间（起）</label>
            <input type="datetime-local" class="form-control" name="startSowTime" value="${startSowTime}">
        </div>
        <div class="col-md-2">
            <label class="form-label">播种时间（止）</label>
            <input type="datetime-local" class="form-control" name="endSowTime" value="${endSowTime}">
        </div>
        <div class="col-md-2">
            <label class="form-label">状态</label>
            <input type="text" class="form-control" name="status" value="${status}" placeholder="如：待采收/已采收">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button type="submit" class="btn btn-primary me-2">筛选</button>
            <a href="${pageContext.request.contextPath}/production/sow_harvest_management" class="btn btn-secondary">重置</a>
        </div>
    </form>

    <!-- 数据列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">播种采收记录列表</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>记录ID</th>
                        <th>基地ID</th>
                        <th>播种数量</th>
                        <th>播种时间</th>
                        <th>采收时间</th>
                        <th>采收数量</th>
                        <th>状态</th>

                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach var="item" items="${sowHarvestList}">
                        <tr>
                            <td>${item.sowHarvestId}</td>
                            <td>${item.baseId}</td>
                            <td>${item.sowCount}</td>
                            <td><fmt:formatDate value="${item.sowTime}" pattern="yyyy-MM-dd HH:mm"/></td>
                            <td><fmt:formatDate value="${item.harvestTime}" pattern="yyyy-MM-dd HH:mm"/></td>
                            <td>${item.harvestCount}</td>
                            <td>${item.status}</td>
                            <td>
<%--                                <button class="btn btn-sm btn-outline-primary me-1" --%>
<%--                                        data-id="${item.sowHarvestId}"--%>
<%--                                        data-baseid="${item.baseId}"--%>
<%--                                        data-sowcount="${item.sowCount}"--%>
<%--                                        data-sowtime="${item.sowTime}"--%>
<%--                                        data-harvesttime="${item.harvestTime}"--%>
<%--                                        data-harvestcount="${item.harvestCount}"--%>
<%--                                        data-status="${item.status}"--%>
<%--                                        onclick="openEditFromBtn(this)">编辑</button>--%>
<%--                                <button class="btn btn-sm btn-outline-danger" onclick="doDelete('${item.sowHarvestId}')">删除</button>--%>
                            </td>
                        </tr>
                    </c:forEach>
                    <c:if test="${empty sowHarvestList}">
                        <tr><td colspan="8" class="text-center">暂无数据</td></tr>
                    </c:if>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 新增模态框 -->
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增播种采收记录</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="addForm">
                    <div class="mb-3">
                        <label class="form-label">记录ID</label>
                        <input type="text" class="form-control" name="sowHarvestId" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">基地ID</label>
                        <input type="number" class="form-control" name="baseId">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">播种数量</label>
                        <input type="number" class="form-control" name="sowCount" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">播种时间</label>
                        <input type="datetime-local" class="form-control" name="sowTime" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">采收时间</label>
                        <input type="datetime-local" class="form-control" name="harvestTime">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">采收数量</label>
                        <input type="number" class="form-control" name="harvestCount">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">状态</label>
                        <input type="text" class="form-control" name="status" placeholder="如：待采收/已采收">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="submitAdd()">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑模态框 -->
<div class="modal fade" id="editModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">编辑播种采收记录</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="editForm">
                    <input type="hidden" name="sowHarvestId">
                    <div class="mb-3">
                        <label class="form-label">基地ID</label>
                        <input type="number" class="form-control" name="baseId">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">播种数量</label>
                        <input type="number" class="form-control" name="sowCount">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">播种时间</label>
                        <input type="datetime-local" class="form-control" name="sowTime">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">采收时间</label>
                        <input type="datetime-local" class="form-control" name="harvestTime">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">采收数量</label>
                        <input type="number" class="form-control" name="harvestCount">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">状态</label>
                        <input type="text" class="form-control" name="status">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="submitEdit()">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
function toDatetimeLocalStr(dateStr) {
    if (!dateStr || dateStr === 'null') return '';
    const d = new Date(dateStr);
    const pad = n => String(n).padStart(2, '0');
    return d.getFullYear() + '-' + pad(d.getMonth()+1) + '-' + pad(d.getDate()) + 'T' + pad(d.getHours()) + ':' + pad(d.getMinutes());
}

function openEdit(id, baseId, sowCount, sowTime, harvestTime, harvestCount, status) {
    // 保留旧API以兼容，如需直接调用
    const form = document.getElementById('editForm');
    form.sowHarvestId.value = id || '';
    form.baseId.value = (baseId || '');
    form.sowCount.value = (sowCount || '');
    form.sowTime.value = toDatetimeLocalStr(sowTime);
    form.harvestTime.value = toDatetimeLocalStr(harvestTime);
    form.harvestCount.value = (harvestCount || '');
    form.status.value = (status || '');
    var modal = new bootstrap.Modal(document.getElementById('editModal'));
    modal.show();
}
// 去掉了编辑
function openEditFromBtn(btn){
    const id = btn.getAttribute('data-id');
    const baseId = btn.getAttribute('data-baseid');
    const sowCount = btn.getAttribute('data-sowcount');
    const sowTime = btn.getAttribute('data-sowtime');
    const harvestTime = btn.getAttribute('data-harvesttime');
    const harvestCount = btn.getAttribute('data-harvestcount');
    const status = btn.getAttribute('data-status');
    openEdit(id, baseId, sowCount, sowTime, harvestTime, harvestCount, status);
}

function submitAdd() {
    const form = $('#addForm');
    const data = {
        sowHarvestId: form.find('[name="sowHarvestId"]').val(),
        baseId: form.find('[name="baseId"]').val() || null,
        sowCount: parseInt(form.find('[name="sowCount"]').val(), 10),
        sowTime: form.find('[name="sowTime"]').val() ? new Date(form.find('[name="sowTime"]').val()) : null,
        harvestTime: form.find('[name="harvestTime"]').val() ? new Date(form.find('[name="harvestTime"]').val()) : null,
        harvestCount: form.find('[name="harvestCount"]').val() ? parseInt(form.find('[name="harvestCount"]').val(), 10) : null,
        status: form.find('[name="status"]').val() || null,
    };
    $.ajax({
        url: '${pageContext.request.contextPath}/production/sow_harvest/add',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function (res) {
            if (res === 'success') { location.reload(); }
        }
    });
}

function submitEdit() {
    const form = $('#editForm');
    const data = {
        sowHarvestId: form.find('[name="sowHarvestId"]').val(),
        baseId: form.find('[name="baseId"]').val() ? parseInt(form.find('[name="baseId"]').val(), 10) : null,
        sowCount: form.find('[name="sowCount"]').val() ? parseInt(form.find('[name="sowCount"]').val(), 10) : null,
        sowTime: form.find('[name="sowTime"]').val() ? new Date(form.find('[name="sowTime"]').val()) : null,
        harvestTime: form.find('[name="harvestTime"]').val() ? new Date(form.find('[name="harvestTime"]').val()) : null,
        harvestCount: form.find('[name="harvestCount"]').val() ? parseInt(form.find('[name="harvestCount"]').val(), 10) : null,
        status: form.find('[name="status"]').val() || null,
    };
    $.ajax({
        url: '${pageContext.request.contextPath}/production/sow_harvest/update',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function (res) {
            if (res === 'success') { location.reload(); }
        }
    });
}

function doDelete(id) {
    if (!confirm('确认删除该记录？')) return;
    $.ajax({
        url: '${pageContext.request.contextPath}/production/sow_harvest/delete',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ sowHarvestId: id }),
        success: function (res) {
            if (res === 'success') { location.reload(); }
        }
    });
}
</script>
<%@ include file="../common/footer.jsp" %> 